package view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import main.Game;
import model.Board;

public class Board_view extends JPanel{
	Game game;
	ImageIcon img;
	static HashMap<String, Color>player_color= new HashMap<>();
	public Board_view(Game g){
		this.game = g;
		for(int i=0;i<g.listPlayer.size();i++){
			if(i==0){
				player_color.put(g.listPlayer.get(i).getName(), Color.blue);
			}
			else if(i==1){
				player_color.put(g.listPlayer.get(i).getName(), Color.green);
			}
			else if(i==2){
				player_color.put(g.listPlayer.get(i).getName(), Color.magenta);
			}
			else if(i==3){
				player_color.put(g.listPlayer.get(i).getName(), Color.yellow);
			}
			else if(i==4){
				player_color.put(g.listPlayer.get(i).getName(), Color.red);
			}
			else if(i==5){
				player_color.put(g.listPlayer.get(i).getName(), Color.orange);
			}
		}
	}

	public JPanel view(){

		//board minimap
		JPanel minimap = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx=0;
		c.gridy=0;
		minimap.setPreferredSize(new Dimension(450,310));
		minimap.setBorder(BorderFactory.createLineBorder(Color.black));


		//pawn info
		JPanel pawn_info = new JPanel();
		pawn_info.setLayout(new GridBagLayout());
		GridBagConstraints gb = new GridBagConstraints();
		pawn_info.setPreferredSize(new Dimension(140,300));
		pawn_info.setBorder(BorderFactory.createLineBorder(Color.black));

		for(int i=0; i<player_color.size();i++){
			JLabel name = new JLabel(game.listPlayer.get(i).getName());
			JPanel color = new JPanel();
			color.setPreferredSize(new Dimension(6,6));
			color.setBackground(player_color.get(game.listPlayer.get(i).getName()));
			color.setBorder(BorderFactory.createLineBorder(Color.black));
			gb.gridx=0;
			gb.gridy=i;
			gb.ipadx = 10;
			gb.ipady=10;
			pawn_info.add(name,gb);
			gb.gridx=1;
			pawn_info.add(color,gb);
		}
		minimap.add(pawn_info,c);


		//the board
		JPanel board = new JPanel();
		board.setPreferredSize(new Dimension(300,300));

		board.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		JPanel cell;
		gbc.gridx=0;
		gbc.gridy=0;
		for(int i=0;i<Board.map.length;i++){


			//position of the cell
			if(i<11){
				gbc.gridx=i;
			}
			else if(i>10 && i<21){
				gbc.gridy=i-10;
			}
			else if(i>20 && i<31){
				gbc.gridx=30-i;
			}
			else if(i>30 && i<40){
				gbc.gridy=40-i;
			}

			//create a cell
			cell = new Cell_view_board(Board.map[i],i);
			board.add(cell,gbc);
		}
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridheight=9;
		gbc.gridwidth=9;
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200,200));
		img = new ImageIcon(new ImageIcon("img/Logo.png").getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
		JLabel l = new JLabel(img);
		panel.add(l);
		board.add(panel,gbc);
		c.gridx=1;
		c.gridy=0;
		minimap.add(board,c);
		return minimap;	
	}
}
